<!-- 新增编辑车辆 -->
<template>
  <div>
    <el-dialog
      :visible="showDialog"
      @close="closeDialog"
      title="新增车辆"
      width="600px"
    >
      <el-form
        label-width="100px"
        :model="diaForm"
        :rules="rulesForm"
        ref="addForm"
      >
        <el-form-item label="车辆类型" prop="truckTypeId">
          <el-select v-model="diaForm.truckTypeId" placeholder="请选择">
            <el-option
              v-for="item in tabelList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车辆号码" prop="licensePlate">
          <el-input
            placeholder="请输入车牌号"
            v-model="diaForm.licensePlate"
          ></el-input>
        </el-form-item>
        <el-form-item label="GPS设备ID" prop="deviceGpsId">
          <el-input
            placeholder="请输入GPS设备ID"
            v-model="diaForm.deviceGpsId"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center" style="margin-bottom:20px">
            <el-col :span="12">
              <el-button type="danger" style="width:90px" @click="addBtn"
                >确认</el-button
              >
              <el-button style="width:90px" @click="closeDialog"
                >取消</el-button
              >
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { truckTypeAdd } from '@/api/transit'
export default {
  name: 'VehicleAdd',
  props: {
    showDialog: {
      type: Boolean,
      required: true
    },
    tabelList: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      // 弹窗表单
      diaForm: {
        licensePlate: '',
        deviceGpsId: '',
        truckTypeId: ''
      },
      // 表单校验
      rulesForm: {
        licensePlate: [
          { required: true, message: '请输入车牌号码', trigger: 'blur' },
          {
            pattern: /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]$/,
            message: '请输入正确的车牌号',
            trigger: 'blur'
          }
        ],
        deviceGpsId: [
          { required: true, message: '请输入车牌GPS设备id', trigger: 'blur' }
        ],
        truckTypeId: [
          { required: true, message: '请输入车辆类型', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 通知父组件关闭表单
    closeDialog(flag = false) {
      this.$emit('closeDialog', flag)
      this.$refs.addForm.resetFields()
    },
    // 添加确认按钮
    addBtn() {
      this.$refs.addForm.validate(async isOk => {
        if (isOk) {
          // 调用添加接口
          await truckTypeAdd(this.diaForm)
          // 提示用户
          this.$message.success('添加成功')
          // 关闭弹窗
          this.closeDialog(true)
        }
      })
    }
  }
}
</script>
<style scoped lang="scss">
::v-deep .el-dialog__body {
  padding: 40px 50px 0 20px !important;
}
</style>
